<?php

class SourceController extends Controller{

    private $M_sources;

    /**
     * 构造方法
     * 初始化
     */
    public function __construct(){
        $this->M_sources = Model::getInstance('sources');
    }

    public function index(){
		global $controller;
		
        $curr_page = (int)$this->input()->post('pageNum',1);
        $per_page  = (int)$this->input()->post('numPerPage',30);

        $title = $this->input()->post('title');
        $filter = array();

        if($title){
            $filter['title'] = $title;
        }

        $list =$this->M_sources->getSourceList($curr_page,$per_page,$filter);
        $data = array(
			'controller'=>$controller,
            'list'=>$list,
        );
        Template::show('source/index.php',$data);
    }

    public function getSources($filter = array(),$flag = false){
        return $this->M_sources->getSources($filter,$flag);
    }

    public function add(){
		global $controller;
		
		$data = array(
			'controller'=>$controller
        );
		
        Template::show('source/add.php',$data);
    }

    public function del(){

        $source_id = $this->input()->get('source_id');
        $ret = $this->M_sources->delSource($source_id);

        if($ret){
            $data = array(
                "statusCode"=>"200",
                "message"=>"删除成功",
                "navTabId"=>"source",
                "callbackType"=>"closeCurrent"
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"删除失败",
            );
            echo Response::JSON(300,$data);
        }
    }

    public function edit(){
		global $controller;
		
        $source_id = (int)$this->input()->get('source_id');

        $source_info = $this->M_sources->get($source_id);
        $data = array(
			'controller'=>$controller,
            'source_info'=>$source_info,
        );

        Template::show('source/edit.php',$data);
    }

    public function ajaxAdd(){

        $title = $this->input()->post('title');
        $site_address = $this->input()->post('site_address');

        if(!$title || !$site_address){
            $data = array(
                "statusCode"=>"300",
                "message"=>"必须填写站点名及站点网址",
            );
            echo Response::JSON(300,$data);
            exit;
        }

        $ret = $this->M_sources->addSource($title, $site_address);
        if($ret){

            $data = array(
                "statusCode"=>"200",
                "message"=>"添加成功",
                "navTabId"=>"source",
                "callbackType"=>"closeCurrent"
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"添加失败",
            );
            echo Response::JSON(300,$data);
        }
        exit;
    }

    public function ajaxEdit(){

        $source_id = $this->input()->post('source_id');
        $title = $this->input()->post('title');
        $site_address = $this->input()->post('site_address');

        if(!$source_id || !$title || !$site_address){
            $data = array(
                "statusCode"=>"300",
                "message"=>"参数错误",
            );
            echo Response::JSON(300,$data);
            exit;
        }

        $ret = $this->M_sources->updateSource($source_id,$title,$site_address);
        if($ret){

            $data = array(
                "statusCode"=>"200",
                "message"=>"修改成功",
                "navTabId"=>"source",
                "callbackType"=>"closeCurrent"
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"修改失败",
            );
            echo Response::JSON(300,$data);
        }
        exit;
    }
}